package com.sample.tdf.api;

import cn.com.taiji.common.api.BaseController;
import cn.com.taiji.common.api.BaseTreeController;
import cn.com.taiji.common.dto.ResultDTO;
import com.sample.tdf.dto.EmployeeDTO;
import com.sample.tdf.dto.OrganizationDTO;
import com.sample.tdf.service.IEmployeeOrganizationService;
import com.sample.tdf.service.IEmployeeService;
import com.sample.tdf.service.IOrganizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Api(tags = {"202.机构操作接口"})
@RequestMapping("/sample/org")
public class OrganizationController extends BaseTreeController<IOrganizationService, OrganizationDTO> {

    @Autowired
    IEmployeeOrganizationService iEmployeeOrganizationService;
    @Autowired
    IEmployeeService iEmployeeService;

    @ApiOperation(value = "根据id所有用户（树状结构）")
    @GetMapping(value = "/getForTree")
    public ResultDTO getForTree(@RequestParam String id) {
        OrganizationDTO rootNode = iBaseService.findById(id);
        List<OrganizationDTO> list = iBaseService.findChildrenByParentId(id);
        list.forEach((item) -> {
            List<String> peopleIds = iEmployeeOrganizationService.findSecondIdsByFirstId(item.getId());
            List<String> tmp = iEmployeeOrganizationService.findFirstIdsBySecondId(item.getId());
            List<EmployeeDTO> peopleDTOList = iEmployeeService.findByIds(peopleIds);
            item.setEmployeeList(peopleDTOList);
        });
        List<OrganizationDTO> data = iBaseService.listToTree(list, rootNode.getParentId());
        return new ResultDTO(data);
    }
}

